Lab8


4531206721_4531207321  นาย ชัยชนะ นิลวัชรารัง และ นาย เฉลิมพงศ์ สัตยาวิบูล (3/9/2545 (11:45:32))
(SM=3, CM=13, ST=14, KY=0, TR=00:02)

TestScript
Mini-Quiz :  (0.0 คะแนน)

JLab>javac Rational.java
JLab>
JLab>java Selftest
>>JLabIO->recripocate : ok
>>JLabIO->recripocate : ok
>>JLabIO->multiply : ok
>>JLabIO->multiply : ok
>>JLabIO->multiply : ok
>>JLabIO->multiply : ok
>>JLabIO->addMatrix : java.lang.NullPointerException
	at Selftest.main(Selftest.java:102)
java.lang.NullPointerException
	at Selftest.main(Selftest.java:102)
Exception : java.lang.NullPointerException
>>JLabIO->addMatrix : Exception : java.lang.NullPointerException

>>JLab:<POINT>6</POINT>

JLab>

ได้ 6 คะแนน
Source Code
// 2110101 : Lab8 (2545)
// dept. of computer engineering
// Chulalongkorn Univ.

import jlab.JLabIO;

public class Rational {
  int numerator;
  int denominator;

  //--------------------------------------------------------
  // an object method returning the recripocal of "this"
  // rational number.
  public Rational recripocate() {
     return new Rational(denominator, numerator);

  }
  //--------------------------------------------------------
  // an object method returning the result of "this"
  // rational number multiplied by a.
  public Rational multiply(Rational a) {
  //    System.out.println(this.numerator);
  int x = gcd(a.numerator, this.denominator);
  a.numerator /= x;
  this.denominator /= x;

  x = gcd(this.numerator, a.denominator);
  this.numerator /= x;
  a.denominator /= x;
  

 a.numerator *= this.numerator;
  a.denominator *= this.denominator;
  
    return a;

  }
  //--------------------------------------------------------
  // a class method for multiplying two matrices of rational
  // numbers (matrices a and b).
  public static Rational[][] mulMatrix(Rational[][] a, Rational[][] b) {
 
    return null;

  }
  //--------------------------------------------------------
  // you can use the main method for your own testing (optional).
  public static void main(String[] args) {
    



  }
  //--------------------------------------------------------
  public Rational() {
    this(0, 1);
  }
  public Rational(int n, int d) {
    int g = gcd(n, d);
    n = n / g;
    d = d / g;
    this.numerator = n;
    this.denominator = d;
  }
  public Rational add(Rational a) {
    int g = gcd(this.denominator, a.denominator);
    int d = this.denominator / g * a.denominator;
    int n = this.numerator * d / this.denominator +
            a.numerator * d / a.denominator;
    return new Rational(n, d);
  }
  public String toString() {
    return this.numerator + "/" + this.denominator;
  }
  public static int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}